package jsu.xxy.coffee3.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.models.security.SecurityScheme;
import jakarta.servlet.http.HttpServletResponse;
import jsu.xxy.coffee3.Utils.ExcelOutUtil;
import jsu.xxy.coffee3.Utils.MD5;
import jsu.xxy.coffee3.domain.Staffs;
import jsu.xxy.coffee3.mapper.StaffsMapper;
import jsu.xxy.coffee3.service.impl.StaffsServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/staff")
public class StaffsController {
    @Autowired
    StaffsMapper mapper;
    @Autowired
    StaffsServiceImpl service;

    @GetMapping("/getStaffsExcel")
    @Operation(summary = "导出excel，店员全部信息")
    public void getExcel(HttpServletResponse response){
        XSSFWorkbook wb = ExcelOutUtil.getDeviceDataByExcelStaffs(mapper.selectList(null));

        String fileName = "deviceData.xlsx";
        OutputStream outputStream = null;
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8");
            //设置ContentType请求信息格式
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName);
            outputStream = response.getOutputStream();
            wb.write(outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @GetMapping("/login")
    @Operation(summary = "员工登录")
    public Boolean login(@RequestParam String phone , @RequestParam String password){
        return service.login(phone,password);

    }
    @GetMapping("/loginManage")
    @Operation(summary = "管理员登录")
    public Boolean loginManage(@RequestParam String phone , @RequestParam String password){
        return service.loginManage(phone,password);
    }
    @GetMapping("/getAllStaffs")
    @Operation(summary = "所有员工信息")
    public IPage<Staffs> getAll(
             @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
    ){
        return service.getAll(pageNo,pageSize);
    }

    @GetMapping("/selectByName")
    @Operation(summary = "通过姓名查询员工")
    public IPage<Staffs> selectByName(@RequestParam("name") String name, @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        return service.selectByName(name,pageNo,pageSize);
    }
    @GetMapping("/selectByPhone")
    @Operation(summary = "通过手机号查询员工")
    public IPage<Staffs> selectByPhone(@RequestParam("phone") String phone, @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        return service.selectByPhone(phone,pageNo,pageSize);
    }
    @GetMapping("/selectById")
    @Operation(summary = "通过id查询员工")
    public IPage<Staffs> selectById(@RequestParam("id") int id, @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        return service.selectById(id , pageNo,pageSize);
    }
    @GetMapping("/selectByStatus")
    @Operation(summary = "通过状态查询员工")
    public IPage<Staffs> selectByStatus(@RequestParam("status") String status, @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        return service.selectByStatus(status,pageNo,pageSize);
    }
    @PostMapping("/change")
    @Operation(summary = "修改员工信息")
    public Boolean change(@RequestBody Staffs staffs){
        return service.change(staffs);

    }

}
